let express = require('express');
let router = express.Router();
//————————————连接数据库——————————
//记得装mysql依赖啊
var mysql = require('mysql');
var connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',//数据库用户名
  password: 'password',//密码
  database: 'bookshop'//数据库名
});

connection.connect((err) => {
  if (err) {
    console.log(err)
  } else {
    console.log("数据库连接成功")
  }

});
//获取客户端数据
router.get('/', function (req, response, next) {
  // 把从客户端传来的名字，密码赋值到新的变量

//   let pwd = req.query.userPassword
//   console.log(name)
//   console.log(pwd)
  let name = req.session.token
  // 通过名字查询数据库用户表得到密码  password是数据表字段名，表名user_info
  connection.query("SELECT passwork, contact, geneder, mail FROM update_info WHERE user = '" + name + "'", function (err, res, fields) {
    // 当没有查找到密码时
    if (err) {
      throw err
      return;
    } else {
      if (res.length > 0) {
        //将数据库传来的数据转化为JSON格式
        let uPasswordString = JSON.stringify(res);
        console.log(res)
        console.log(uPasswordString)
        let userPassword = JSON.parse(uPasswordString)[0].passwork;
        let contact = JSON.parse(uPasswordString)[0].contact;
        let geneder = JSON.parse(uPasswordString)[0].geneder;
        let mail = JSON.parse(uPasswordString)[0].mail;
        console.log(userPassword)
        console.log(contact)
        console.log(geneder)
        console.log(mail)
        let data = {
          "name" : name,
          "suserPassword" : userPassword,
          "contact" : contact,
          "geneder" : geneder,
          "mail" : mail,
        }
        // if (userPassword == pwd) {
        //   //将数据库的密码userPassword 和服务端 传来的密码相等
          response.send({ code: 200, msg: '登录成功' , data: data})
          return
        //   return
        // } else {
        //   console.log(userPassword)
        //   console.log(pwd)
          // response.send({ statusCode: 0, msg: '密码不正确' })
        //   return
        // }
      }
    }
    //如果没有数据，说明用户名填写错误 
    response.send({ statusCode: 0, msg: '用户名不正确' })
  })
});

// router.get('/', function (req, res, next) {
//     // Handle file upload
//     let obj = null,
//         data = {
//             code: 0,
//             msg: '请求成功',
//             data: {}
//         }
//     var currentData = ''

//     req.on('data', (chunk) => {
//         currentData += chunk
//     })

//     req.on('end', () => {
//         obj = querystring.parse(currentData)
        
//         console.info('==post obj==', obj)
//         console.log(obj.name)
//         let name = obj.name;
//         let pwd = obj.pass;
//         let contact = obj.contact;
//         let geneder = obj.geneder;
//         let mail = obj.mail;
        
//         connection.query("SELECT * FROM update_info WHERE user = '" + name + "'", function (err, ress, fields) {

//         let uName = ress[0]

//         if (uName == undefined) {
//             //    不存在时插入数据
//             connection.query('INSERT INTO update_info SET  ?', { user: name, passwork: pwd, contact: contact, geneder: geneder, mail: mail}, function (err, data) {
//                 if (err) {
//                     console.log(err)
//                     res.send({ statusCode: 0, msg: '更新失败，请重试' })
//                     return
//                 } else {
//                     res.send({ statusCode: 200, msg: '更新成功' })
//                     return
//                 }
//             })
//         } else {

//             res.send({ code: 0, msg: '用户名已存在' })
//         }
//     })
//         // res.writeHead(200, {
//         //     "Content-Type": "text/html;charset=utf-8"
//         // })
//         // res.end(JSON.stringify(data))
//     })
//     // console.log(req.file); // Information about the uploaded file
//     // res.send('File uploaded successfully!');
//   });
module.exports = router;
